package cn.goktech.sports.modules.importDataone.service.impl;

import cn.goktech.sports.common.entity.Page;
import cn.goktech.sports.common.entity.Query;
import cn.goktech.sports.common.entity.R;
import cn.goktech.sports.common.utils.CommonUtils;
import cn.goktech.sports.modules.importDataone.dao.ImportDataoneMapper;
import cn.goktech.sports.modules.importDataone.dao.UserMapper;
import cn.goktech.sports.modules.importDataone.entity.*;
import cn.goktech.sports.modules.importDataone.service.ImportDataoneService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * 模板一数据
 *
 * @author ZhouChenglin<yczclcn @ 1 6 3 . com>
 */
@Service("importDataoneService")
public class ImportDataoneServiceImpl implements ImportDataoneService {

    @Autowired(required = false)
    private ImportDataoneMapper importDataoneMapper;

    @Autowired(required = false)
    private UserMapper userMapper;


    public static Map<String, Long> productMap = new HashMap<>();

    /**
     * 分页查询
     *
     * @param params
     * @return
     */
    @Override
    public Page<ImportDataoneEntity> listImportDataone(Map<String, Object> params) {
        Query query = new Query(params);
        Page<ImportDataoneEntity> page = new Page<>(query);
        importDataoneMapper.listForPage(page, query);
        return page;
    }

    /**
     * 新增
     *
     * @param importDataone
     * @return
     */
    @Override
    public R saveImportDataone(ImportDataoneEntity importDataone) {
        int count = importDataoneMapper.save(importDataone);
        return CommonUtils.msg(count);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public R getImportDataoneById(Long id) {
        ImportDataoneEntity importDataone = importDataoneMapper.getObjectById(id);
        return CommonUtils.msg(importDataone);
    }

    /**
     * 修改
     *
     * @param importDataone
     * @return
     */
    @Override
    public R updateImportDataone(ImportDataoneEntity importDataone) {
        int count = importDataoneMapper.update(importDataone);
        return CommonUtils.msg(count);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public R batchRemove(Long[] id) {
        int count = importDataoneMapper.batchRemove(id);
        return CommonUtils.msg(id, count);
    }

    /**
     * 导入DHL数据
     *
     * @param strings
     * @return
     */
    @Override
    public R addDHL(List<String[]> strings) {
        List<NationArea> nationAreas = new ArrayList<>();
        //获取产品列表
        getProductMaps();
        Map<Double, WeightLimit> weightLimitMap = getWeightLimitMap(strings.get(0)[1]);
        Map<String, Standhard> standhardMap = getStandhardMap();
        List<WeightLimit> weightLimitList = importDataoneMapper.getWeightLimit(strings.get(0)[1]);
        Map<String, Long> countryPartitionId = getCountryPartition();
        if (weightLimitList == null || weightLimitList.size() == 0) {
            return R.error("请先导入限重数据");
        }
        if (productMap.get(strings.get(0)[1]) == null) {
            return R.ok("产品名称有误");
        }
        //导入的文档，数据从第四行开始取数据。
        for (int i = 4; i < strings.size(); i++) {
            NationArea nationArea = new NationArea();
            try {
                //设置分区id
                nationArea.setCountryPartitionId(countryPartitionId.get(strings.get(i)[0]) == null ? 0L : countryPartitionId.get(strings.get(i)[0]));
                nationArea.setNationalAreaName(strings.get(i)[1]);
                nationArea.setFilePrice(BigDecimal.valueOf(Double.valueOf(strings.get(i)[2])));
                nationArea.setFileHeavyPrice(BigDecimal.valueOf(Double.valueOf(strings.get(i)[3])));
                nationArea.setGoodsPrice(BigDecimal.valueOf(Double.valueOf(strings.get(i)[4])));
                if (StringUtils.isNotBlank(strings.get(i)[5]) && !strings.get(i)[5].equals("无")) {
                    nationArea.setGoodsHeavyPrice(BigDecimal.valueOf(Double.valueOf(strings.get(i)[5])));
                }
//                nationArea.setGoodsHeavyPrice(BigDecimal.valueOf(Double.valueOf(strings.get(i)[5])));
                //设置参考时限
                if (StringUtils.isNotBlank(strings.get(i)[6]) && !strings.get(i)[6].equals("无")) {
                    nationArea.setConsultExtension(strings.get(i)[6]);
                }

                //设置限重id。如果限重数据为无,取限重List第一条数据
                if (strings.get(i)[7].equals("无")) {
                    nationArea.setContinuousWeightId(weightLimitList.get(0).getWeightLimitId() == null ? 0L : weightLimitList.get(0).getWeightLimitId());
                } else {
                    if (weightLimitMap.get(Double.valueOf(strings.get(i)[7])) != null) {
                        nationArea.setContinuousWeightId(weightLimitMap.get(Double.valueOf(strings.get(i)[5])).getWeightLimitId());
                    }
                }
                //设置尺寸标准数据
                if (standhardMap.get(strings.get(0)[1]) != null && standhardMap.get(strings.get(0)[1]).getStandardId() != null) {
                    nationArea.setStandardId(standhardMap.get(strings.get(0)[1]).getStandardId());
                }
                //设置文件最高限重
                nationArea.setFileWeight(Double.valueOf(strings.get(i)[9] == null ? "999999" : strings.get(i)[9]));
                //设置开始采用计费区间
                nationArea.setStartSection(Double.valueOf(strings.get(i)[10] == null ? "" : strings.get(i)[10]));

            } catch (Exception E) {
                return R.ok("数据异常，导入失败");
            }
            nationArea.setArticleTypeId(productMap.get(strings.get(0)[1]));
            nationAreas.add(nationArea);
        }

        //插入最新的数据
        if (nationAreas != null && nationAreas.size() > 0) {
            //清空之前的数据。
            importDataoneMapper.deleteNationArea(productMap.get(strings.get(0)[1]));
            importDataoneMapper.importExcel(nationAreas);
        }
        return R.ok("导入成功");
    }


    /**
     * 导入EMS数据
     *
     * @param strings
     * @return
     */
    @Override
    @Transactional
    public R addEMS(List<String[]> strings) {
        //获取产品列表
        getProductMaps();
        if (productMap.get(strings.get(0)[0]) == null) {
            return R.error("产品名称有误");
        }
        Map<Double, WeightLimit> weightLimitMap = getWeightLimitMap(strings.get(0)[0]);
        Map<String, Standhard> standhardMap = getStandhardMap();
        List<NationArea> nationAreas = new ArrayList<>();
        for (int i = 4; i < strings.size(); i++) {
            NationArea nationArea = new NationArea();
            try {
                nationArea.setNationalAreaName(strings.get(i)[0]);
                nationArea.setFilePrice(BigDecimal.valueOf(Double.valueOf(strings.get(i)[1])));
                nationArea.setGoodsPrice(BigDecimal.valueOf(Double.valueOf(strings.get(i)[2])));
                nationArea.setGoodsHeavyPrice(BigDecimal.valueOf(Double.valueOf(strings.get(i)[3])));
                nationArea.setFileHeavyPrice(BigDecimal.valueOf(Double.valueOf(strings.get(i)[3])));
                nationArea.setConsultExtension(strings.get(i)[4]);
                //设置限重Id
                if (weightLimitMap.get(Double.valueOf(strings.get(i)[5])) == null) {
                    return R.error("请先导入限重数据");
                }
                nationArea.setContinuousWeightId(weightLimitMap.get(Double.valueOf(strings.get(i)[5])).getWeightLimitId());
                if (standhardMap.get(strings.get(i)[6]) == null) {
                    return R.error("请先导入尺寸标准数据");
                }
                nationArea.setStandardId(standhardMap.get(strings.get(i)[6]).getStandardId());
                //设置附加费
                if (StringUtils.isNotBlank(strings.get(i)[7])) {
                    nationArea.setSurcharge(BigDecimal.valueOf(Double.valueOf(strings.get(i)[7])));
                }
                //设置计抛长度
                if (StringUtils.isNotBlank(strings.get(i)[8])) {
                    nationArea.setMarkThrow(Double.valueOf(strings.get(i)[8]));
                }
            } catch (Exception E) {
                return R.error("数据异常，导入失败");
            }
            //设置固定值
            nationArea.setArticleTypeId(productMap.get(strings.get(0)[0]));
            nationAreas.add(nationArea);
        }
        //批量新增数据。
        if (nationAreas != null && nationAreas.size() > 0) {
            //清空之前的数据。
            importDataoneMapper.deleteNationArea(productMap.get(strings.get(0)[0]));
            importDataoneMapper.importExcel(nationAreas);
        }
        return R.ok("导入成功");
    }

    /**
     * 导入E特快数据
     *
     * @param strings
     * @return
     */
    @Override
    @Transactional
    public R addEFastest(List<String[]> strings) {
        //获取产品列表
        getProductMaps();
        if (productMap.get(strings.get(0)[0]) == null) {
            return R.error("产品名称有误");
        }
        Map<Double, WeightLimit> weightLimitMap = getWeightLimitMap(strings.get(0)[0]);
        Map<String, Standhard> standhardMap = getStandhardMap();
        List<NationArea> nationAreas = new ArrayList<>();
        for (int i = 4; i < strings.size(); i++) {
            NationArea nationArea = new NationArea();
            try {
                nationArea.setNationalAreaName(strings.get(i)[0]);
                nationArea.setGoodsPrice(BigDecimal.valueOf(Double.valueOf(strings.get(i)[1])));
                nationArea.setGoodsHeavyPrice(BigDecimal.valueOf(Double.valueOf(strings.get(i)[2])));
                if (weightLimitMap.get(Double.valueOf(strings.get(i)[3])) == null) {
                    return R.error("请先导入限重数据");
                }
                //设置限重Id
                nationArea.setContinuousWeightId(weightLimitMap.get(Double.valueOf(strings.get(i)[3])).getWeightLimitId());
                if (standhardMap.get(strings.get(i)[4]) == null) {
                    return R.error("请先导入尺寸标准数据");
                }
                //设置尺寸标准数据
                nationArea.setStandardId(standhardMap.get(strings.get(i)[4]).getStandardId());
                if (StringUtils.isNotBlank(strings.get(i)[5]) && !strings.get(i)[5].equals("无")) {
                    nationArea.setConsultExtension(strings.get(i)[5]);

                }
                //设置计泡数据
                nationArea.setMarkThrow(Double.valueOf(strings.get(i)[6] == null ? "" : strings.get(i)[6]));
            } catch (Exception E) {
                return R.error("数据异常，导入失败");
            }
            //设置固定值
            nationArea.setArticleTypeId(productMap.get(strings.get(0)[0]));
            nationAreas.add(nationArea);
        }
        //批量插入数据。
        if (nationAreas != null && nationAreas.size() > 0) {
            //清空之前的数据。
            importDataoneMapper.deleteNationArea(productMap.get(strings.get(0)[0]));
            importDataoneMapper.importExcel(nationAreas);
        }
        return R.ok("导入成功");
    }


    /**
     * 导入航空运输数据
     *
     * @param strings
     * @return
     */
    @Override
    @Transactional
    public R addAviationTransport(List<String[]> strings) {
        List<Aviation> aviationList = new ArrayList<>();
        List<RegisteredAirmail> registeredAirmailList = new ArrayList<>();
        Map<Double, WeightLimit> registeredAirmailWeightLimitMap = getWeightLimitMap(strings.get(0)[3]);
        Map<Double, WeightLimit> aviationAirmailWeightLimitMap = getWeightLimitMap(strings.get(0)[1]);
        Map<String, Standhard> standardMap = getStandhardMap();
        getProductMaps();
        if (productMap.get(strings.get(0)[1]) == null || productMap.get(strings.get(0)[3]) == null) {
            return R.error("对应产品名称有误");
        }

        for (int i = 4; i < strings.size(); i++) {
            Aviation aviation = new Aviation();
            RegisteredAirmail registeredAirmail = new RegisteredAirmail();
            try {
                //设置航空平常小包数据
                aviation.setArea_name(strings.get(i)[0]);
                aviation.setBasic_postage_usual(BigDecimal.valueOf(Double.valueOf(strings.get(i)[1])));
                aviation.setBasic_postage_usual_above(BigDecimal.valueOf(Double.valueOf(strings.get(i)[2])));
                //设置标准id
                if (standardMap.get(strings.get(0)[1]) == null) {
                    return R.error("请先导入尺寸标准数据");
                }
                aviation.setStandard_id(standardMap.get(strings.get(0)[1]).getStandardId());

                //设置限重Id
                if (aviationAirmailWeightLimitMap.get(Double.valueOf(strings.get(i)[10])) == null) {
                    return R.error("请先导入限重数据");
                }
                aviation.setWeight_limit_id(aviationAirmailWeightLimitMap.get(Double.valueOf(strings.get(i)[10])).getWeightLimitId());

                //设置参考时限
                aviation.setReference_time(strings.get(i)[9] == null ? "" : strings.get(i)[9]);
                aviation.setProduct_id(productMap.get(strings.get(0)[1]));
                //航空挂号/跟踪小包数据表
                registeredAirmail.setNational_area_name(strings.get(i)[0] == null ? "" : strings.get(i)[0]);
                registeredAirmail.setBasic_postage_thirty(BigDecimal.valueOf(Double.valueOf(strings.get(i)[3])));
                registeredAirmail.setRegistration_fee_thirty(BigDecimal.valueOf(Double.valueOf(strings.get(i)[4])));
                registeredAirmail.setBasic_postage_three_hundred(BigDecimal.valueOf(Double.valueOf(strings.get(i)[5])));
                registeredAirmail.setRegistration_fee_three_hundred(BigDecimal.valueOf(Double.valueOf(strings.get(i)[6])));
                registeredAirmail.setBasic_postage_other(BigDecimal.valueOf(Double.valueOf(strings.get(i)[7])));
                registeredAirmail.setRegistration_fee_other(BigDecimal.valueOf(Double.valueOf(strings.get(i)[8])));
                //设置标准id
                if (standardMap.get(strings.get(0)[3]) == null) {
                    return R.error("请先导入尺寸标准数据");
                }
                registeredAirmail.setStandard_id(standardMap.get(strings.get(0)[3]).getStandardId());
                //设置参考时限
                registeredAirmail.setReference_time(strings.get(i)[9]);
                registeredAirmail.setProduct_id(productMap.get(strings.get(0)[3]));
                //设置限重Id
                if (registeredAirmailWeightLimitMap.get(Double.valueOf(strings.get(i)[10])) != null) {
                    registeredAirmail.setWeight_limit_id(registeredAirmailWeightLimitMap.get(Double.valueOf(strings.get(i)[10])).getWeightLimitId());
                }

            } catch (Exception E) {
                return R.error("数据异常，导入失败");
            }
            aviationList.add(aviation);
            registeredAirmailList.add(registeredAirmail);

        }

        if (aviationList != null && aviationList.size() > 0) {
            importDataoneMapper.deleteAviation();
            importDataoneMapper.addAviation(aviationList);
        }
        if (registeredAirmailList != null && registeredAirmailList.size() > 0) {
            importDataoneMapper.deleteRegisteredAirmail();
            importDataoneMapper.addRegisteredAirmail(registeredAirmailList);
        }
        return R.ok("导入成功");
    }


    /**
     * 获取产品限重信息
     *
     * @param
     * @return
     */
    public Map getWeightLimitMap(String productName) {
        List<WeightLimit> weightLimits = importDataoneMapper.getWeightLimit(productName);
        Map<Double, WeightLimit> weightLimitMap = new HashMap<>();
        if (weightLimits != null && weightLimits.size() > 0) {
            for (int i = 0; i < weightLimits.size(); i++) {
                weightLimitMap.put(weightLimits.get(i).getWeightLimitScale(), weightLimits.get(i));
            }
        }
        return weightLimitMap;
    }

    /**
     * 获取产品限重信息
     *
     * @param
     * @return
     */
    public Map getFirstWeightMap(String productName) {
        List<WeightLimit> weightLimits = importDataoneMapper.getWeightLimit(productName);
        Map<Double, WeightLimit> weightLimitMap = new HashMap<>();
        if (weightLimits != null && weightLimits.size() > 0) {
            for (int i = 0; i < weightLimits.size(); i++) {
                weightLimitMap.put(weightLimits.get(i).getFirstWeight(), weightLimits.get(i));
            }
        }
        return weightLimitMap;
    }

    /**
     * 获取产品标准信息
     *
     * @param
     * @return
     */
    public Map getStandhardMap() {
        List<Standhard> standhardList = importDataoneMapper.getStandhard();
        Map<String, Standhard> standhardMap = new HashMap<>();
        if (standhardList != null && standhardList.size() > 0) {
            for (int i = 0; i < standhardList.size(); i++) {
                standhardMap.put(standhardList.get(i).getStandardName(), standhardList.get(i));
            }
        }
        return standhardMap;
    }

    /**
     * 导入产品标准信息
     *
     * @param
     * @return
     */
    @Override
    public R addStandard(List<String[]> strings) {
        List<Standhard> standardList = new ArrayList<>();
        for (int i = 2; i < strings.size(); i++) {
            Standhard standhard = new Standhard();
            try {
                if (StringUtils.isNotBlank(strings.get(i)[0])) {
                    standhard.setMaxLong(Long.valueOf(strings.get(i)[0]));
                }
                if (StringUtils.isNotBlank(strings.get(i)[1])) {
                    standhard.setMaxWidth(Long.valueOf(strings.get(i)[1]));
                }
                if (StringUtils.isNotBlank(strings.get(i)[2])) {
                    standhard.setMaxHigh(Long.valueOf(strings.get(i)[2]));
                }
                if (StringUtils.isNotBlank(strings.get(i)[3])) {
                    standhard.setMinLong(Long.valueOf(strings.get(i)[3]));
                }
                if (StringUtils.isNotBlank(strings.get(i)[4])) {
                    standhard.setMinWidth(Long.valueOf(strings.get(i)[4]));
                }
                if (StringUtils.isNotBlank(strings.get(i)[5])) {
                    standhard.setMinHigh(Long.valueOf(strings.get(i)[5]));
                }
                if (StringUtils.isNotBlank(strings.get(i)[6])) {
                    standhard.setStandardName(strings.get(i)[6]);
                }
                if (StringUtils.isNotBlank(strings.get(i)[7])) {
                    standhard.setStandardSumSide(Long.valueOf(strings.get(i)[7]));
                }
                if (!strings.get(i)[8].equals("无")) {
                    standhard.setTotalSum(Long.valueOf(strings.get(i)[8]));
                }
                standardList.add(standhard);
            } catch (Exception E) {
                return R.error("数据异常，导入失败");
            }

        }
        if (standardList != null && standardList.size() > 0) {
            importDataoneMapper.deleteStandard();
            importDataoneMapper.addStandard(standardList);
        }
        return R.ok("导入成功");
    }

    @Override
    @Transactional
    public R addLevelCountry(List<String[]> strings) {
        //清空之前的数据
        List<BaseCountry> countryList = new ArrayList<>(strings.size());
        //第一遍循环插入分区数据。
        for (int i = 1; i < strings.size(); i++) {
            BaseCountry baseCountry = new BaseCountry();
            baseCountry.setCountryName(strings.get(i)[0]);
            baseCountry.setParentId(0L);
            countryList.add(baseCountry);
        }
        importDataoneMapper.addBaseCountry(countryList);
        //获取分区数据id
        List<BaseCountry> baseCountryList = userMapper.getCountryList();
        Map<String, Long> map = new HashMap<>();
        for (int i = 0; i < baseCountryList.size(); i++) {
            map.put(baseCountryList.get(i).getCountryName(), baseCountryList.get(i).getCountryId());
        }

        //插入分区国家
        List<BaseCountry> countryLists = new ArrayList<>();
        for (int i = 1; i < strings.size(); i++) {
            //拆分国家
            String[] countrys = strings.get(i)[1].split(",");
            for (int j = 0; j < countrys.length; j++) {
                BaseCountry baseCountry = new BaseCountry();
                if (StringUtils.isNotBlank(countrys[j])) {
                    baseCountry.setCountryName(countrys[j]);
                }
                baseCountry.setParentId(map.get(strings.get(i)[0]));
                countryLists.add(baseCountry);
            }
        }

        if (countryList != null && countryList.size() > 0) {
            importDataoneMapper.deleteBaseCountry();
            importDataoneMapper.addBaseCountry(countryLists);
        }
        return R.ok("导入成功");
    }

    @Override
    public R addWeightLimit(List<String[]> strings) {
        List<WeightLimit> weightLimitList = new ArrayList<>(strings.size());
        for (int i = 2; i < strings.size(); i++) {
            WeightLimit weightLimit = new WeightLimit();
            try {
                if (StringUtils.isNotBlank(strings.get(i)[0])) {
                    weightLimit.setContinuousWeightNum(Double.valueOf(strings.get(i)[0]));
                }
                if (StringUtils.isNotBlank(strings.get(i)[1])) {
                    weightLimit.setWeightLimitScale(Double.valueOf(strings.get(i)[1]));
                } else {
                    if (strings.get(i)[1].contains("DHL")) {
                        weightLimit.setWeightLimitScale(99999.0);
                    }
                }
                if (StringUtils.isNotBlank(strings.get(i)[2])) {
                    weightLimit.setFirstWeight(Double.valueOf(strings.get(i)[2]));
                }
                if (StringUtils.isNotBlank(strings.get(i)[3])) {
                    weightLimit.setProductName(strings.get(i)[3]);
                }
                weightLimitList.add(weightLimit);
            } catch (Exception E) {
                return R.error("数据异常，导入失败");
            }
        }
        if (weightLimitList != null && weightLimitList.size() > 0) {
            //删除之前的限重数据
            importDataoneMapper.deleteWeightLimit();
            importDataoneMapper.addWeightLimit(weightLimitList);
        }
        return R.ok("导入成功");
    }


    @Override
    @Transactional
    public R addCountry(List<String[]> strings) {
        List<BaseCountry> countryList = new ArrayList<>(strings.size());
        for (int i = 2; i < strings.size(); i++) {
            if (strings.get(i)[0] != null || StringUtils.isBlank(strings.get(i)[0])) {
                BaseCountry baseCountry = new BaseCountry();
                baseCountry.setCountryName(strings.get(i)[0] == null ? "" : strings.get(i)[0]);
                baseCountry.setParentId(0L);
                countryList.add(baseCountry);
            }
        }
        if (countryList != null && countryList.size() > 0) {
            //清空之前的数据
            importDataoneMapper.deleteBaseCountry();
            importDataoneMapper.addCountry(countryList);
        }
        return R.ok("导入成功");
    }

    @Override
    public R addBaseProhibited(List<String[]> strings) {
        //清除之前的数据
        importDataoneMapper.deleteBaseProhibited();
        //第一步获取国家列表
        List<BaseCountry> countryList = userMapper.getCountryList();
        List<BaseProhibited> baseProhibitedList = new ArrayList<>();
        Map<String, Long> countryMap = new HashMap<>(countryList.size());
        for (int i = 0; i < countryList.size(); i++) {
            countryMap.put(countryList.get(i).getCountryName(), countryList.get(i).getCountryId());
        }
        for (int i = 0; i < strings.size(); i++) {
            BaseProhibited baseProhibited = new BaseProhibited();
            baseProhibited.setName(strings.get(i)[1] == null ? "" : strings.get(i)[1]);
            if (countryMap.get(strings.get(i)[0] == null ? "" : strings.get(i)[0]) != null) {
                baseProhibited.setAreaId(countryMap.get(strings.get(i)[0]));
                if (strings.get(i).length>2){
                    baseProhibited.setRemark(strings.get(i)[2]);
                }
                baseProhibitedList.add(baseProhibited);
            }
        }
        if (baseProhibitedList.size() > 0) {
            importDataoneMapper.addBaseProhibited(baseProhibitedList);
        }
        return R.ok("导入成功");
    }

    @Override
    public R addProhibitedArticle(List<String[]> strings) {
        //清除之前的数据
        importDataoneMapper.deleteHeaderProhibitedArticle();
        //第一步获取国家列表
        List<Depot> depotList = importDataoneMapper.getDepotList();
        List<HeaderProhibitedArticle> headerProhibitedArticles = new ArrayList<>();
        Map<String, Long> depotMap = new HashMap<>(depotList.size());
        for (int i = 0; i < depotList.size(); i++) {
            depotMap.put(depotList.get(i).getDepotName(), depotList.get(i).getDepotId());
        }
        for (int i = 1; i < strings.size(); i++) {
            HeaderProhibitedArticle headerProhibitedArticle = new HeaderProhibitedArticle();
            headerProhibitedArticle.setProhibitedType(strings.get(i)[1] == null ? "" : strings.get(i)[1]);
            if (depotMap.get(strings.get(i)[0] == null ? "" : strings.get(i)[0]) != null) {
                headerProhibitedArticle.setDepotId(depotMap.get(strings.get(i)[0]));
                headerProhibitedArticles.add(headerProhibitedArticle);
            }
        }

        if (headerProhibitedArticles.size() > 0) {
            importDataoneMapper.addProhibitedArticle(headerProhibitedArticles);
        }
        return R.ok("导入成功");
    }

    @Override
    public List<WeightLimit> getWeightLimit(String productName) {
        return importDataoneMapper.getWeightLimit(productName);
    }

    @Override
    public R addDepot(List<String[]> strings) {
        //清空之前的数据
        importDataoneMapper.deleteDepot();
        List<BaseCountry> countryList = userMapper.getCountryList();
        Map<String, Long> countryMap = new HashMap<>(countryList.size());
        for (int i = 0; i < countryList.size(); i++) {
            countryMap.put(countryList.get(i).getCountryName(), countryList.get(i).getCountryId());
        }
        List<Depot> depotList = new ArrayList<>(strings.size() - 1);
        for (int i = 1; i < strings.size(); i++) {
            if (strings.get(i)[0] != null || StringUtils.isBlank(strings.get(i)[0])) {
                Depot depot = new Depot();
                if (countryMap.get(strings.get(i)[0]) != null) {
                    depot.setCountryId(countryMap.get(strings.get(i)[0]));
                    depot.setDepotName(strings.get(i)[1] == null ? "" : strings.get(i)[1]);
                    depotList.add(depot);
                }
            }
        }
        if (depotList != null && depotList.size() > 0) {
            importDataoneMapper.addDepot(depotList);
        }
        return R.ok("导入成功");
    }

    @Override
    public R addCountryPartition(List<String[]> strings) {
        List<CountryPartition> list = new ArrayList<>(strings.size());
        for (int i = 1; i < strings.size(); i++) {
            if (strings.get(i)[1] != null || StringUtils.isBlank(strings.get(i)[1])) {
                CountryPartition countryPartition = new CountryPartition();
                countryPartition.setPartitionName(strings.get(i)[1]);
                list.add(countryPartition);
            }
        }
        if (list != null && list.size() > 0) {
            importDataoneMapper.deleteCountryPartition();
            importDataoneMapper.addCountryPartition(list);
        }
        return R.ok("导入成功");
    }

    @Override
    public R addProductPartition(List<String[]> strings) {
        List<ProductType> productTypes = importDataoneMapper.getProductList();
        Map<String, Long> productMap = new HashMap<>(productTypes.size());
        for (int i = 0; i < productTypes.size(); i++) {
            productMap.put(productTypes.get(i).getProductName(), productTypes.get(i).getId());
        }
        if (productMap.get(strings.get(0)[0]) == null) {
            return R.error("该产品不存在，请先导入产品");
        }
        Map<String, Long> countryPartitionMap = getCountryPartition();

        List<ProductPartition> productPartitionList = new ArrayList<>();
        for (int i = 2; i < strings.size(); i++) {
            if (StringUtils.isBlank(strings.get(i)[1])) {
                return R.error("国家名称不能为空");
            }
            if (StringUtils.isBlank(strings.get(i)[0])) {
                return R.error("所属分区不能为空");
            }
            if (countryPartitionMap.get(strings.get(i)[0]) != null) {
                ProductPartition productPartition = new ProductPartition();
                productPartition.setPartitionId(countryPartitionMap.get(strings.get(i)[0]));
                productPartition.setCountryName(strings.get(i)[1]);
                productPartition.setProductId(productMap.get(strings.get(0)[0]));
                productPartitionList.add(productPartition);
            }
        }
        if (productPartitionList != null && productPartitionList.size() > 0) {
            importDataoneMapper.deleteProductPartition(productMap.get(strings.get(0)[0]));
            importDataoneMapper.addProductPartition(productPartitionList);
        }
        return R.ok("导入成功");
    }

    @Override
    public R addThroughSectionCharging(List<String[]> strings) {
        Map<String, Long> productMaps = getProductMaps();
        Map<String, Long> countryPartitionMaps = getCountryPartition();
        List<ThroughSectionCharging> throughSectionChargings = new ArrayList<>(strings.size());
        for (int i = 2; i < strings.size(); i++) {
            ThroughSectionCharging t = new ThroughSectionCharging();
            //设置最小重量
            if (StringUtils.isNotBlank(strings.get(i)[0])) {
                t.setMinWeight(Double.valueOf(strings.get(i)[0]));
            }
            //设置最大重量
            if (StringUtils.isNotBlank(strings.get(i)[1])) {
                t.setMaxWeight(Double.valueOf(strings.get(i)[1]));
            }

            //设置金额
            if (StringUtils.isNotBlank(strings.get(i)[2])) {
                BigDecimal bigDecimal = new BigDecimal(strings.get(i)[2]);
                t.setWeightPrice(bigDecimal);
            }
            //插入国家分区数据
            if (StringUtils.isBlank(strings.get(i)[3]) || countryPartitionMaps.get(strings.get(i)[3]) == null) {
                return R.error("请先插入国家分区数据");
            }
            t.setCountryPartitionId(countryPartitionMaps.get(strings.get(i)[3]));

            //插入产品id数据
            if (StringUtils.isBlank(strings.get(i)[4]) || productMaps.get(strings.get(i)[4]) == null) {
                return R.error("请先插入产品数据");
            }
            t.setProductTypeId(productMaps.get(strings.get(i)[4]));
            throughSectionChargings.add(t);
        }
        if (throughSectionChargings != null && throughSectionChargings.size() > 0) {
            importDataoneMapper.deleteThroughSectionCharging();
            importDataoneMapper.addThroughSectionCharging(throughSectionChargings);
        }
        return R.ok("导入成功");
    }

    @Override
    public R addFedEx(List<String[]> strings) {
        Map<String, Long> productMaps = getProductMaps();
        Map<String, Long> countryPartitionMaps = getCountryPartition();
        List<NationArea> nationAreaList = new ArrayList<>(strings.size());
        Map<Double, WeightLimit> weightLimitMap = getFirstWeightMap(strings.get(0)[0]);
        for (int i = 4; i < strings.size(); i++) {
            NationArea t = new NationArea();
            //设置分区id
            if (StringUtils.isNotBlank(strings.get(i)[0]) && countryPartitionMaps.get(strings.get(i)[0]) != null) {
                t.setCountryPartitionId(countryPartitionMaps.get(strings.get(i)[0]));
            }
            //设置通达地区
            if (StringUtils.isNotBlank(strings.get(i)[1])) {
                t.setNationalAreaName(strings.get(i)[1]);
            }
            //设置物品首重
            if (StringUtils.isNotBlank(strings.get(i)[3])) {

                BigDecimal tempValue = new BigDecimal(strings.get(i)[3]);
                t.setGoodsPrice(tempValue);
            }
            //设置物品续重
            if (StringUtils.isNotBlank(strings.get(i)[5])) {
                BigDecimal tempValue = new BigDecimal(strings.get(i)[5]);
                t.setGoodsHeavyPrice(tempValue);
            }
            t.setConsultExtension(strings.get(i)[6] == null ? "无" : strings.get(i)[6]);
            //设置燃油费率
            if (StringUtils.isNotBlank(strings.get(i)[7])) {
                t.setFuelCost(BigDecimal.valueOf(Double.valueOf(strings.get(i)[7]) + 1));
            }

            //设置最高限重
            if (StringUtils.isBlank(strings.get(i)[11]) || weightLimitMap.get(Double.valueOf(strings.get(i)[11])) == null) {
                return R.error("请先导入限重数据");
            }
            t.setContinuousWeightId(weightLimitMap.get(Double.valueOf(strings.get(i)[11])).getWeightLimitId());
            //设置开始采用计费区间.
            if (StringUtils.isNotBlank(strings.get(i)[9])) {
                t.setStartSection(Double.valueOf(strings.get(i)[9]));
            }
            t.setArticleTypeId(productMaps.get(strings.get(0)[0]));
            nationAreaList.add(t);
        }
        if (nationAreaList != null && nationAreaList.size() > 0) {
            importDataoneMapper.deleteNationArea(productMaps.get(strings.get(0)[0]));
            importDataoneMapper.importExcel(nationAreaList);
        }
        return R.ok("导入成功");
    }

    @Override
    public R addFed(List<String[]> strings) {
        Map<String, Long> productMaps = getProductMaps();
        Map<String, Long> countryPartitionMaps = getCountryPartition();
        List<FedExWeightPrice> fedExWeightPriceArrayList = new ArrayList<>(strings.size() * strings.get(1).length);

        for (int j = 2; j < strings.size(); j++) {
            String[] tempArray = strings.get(j);
            for (int i = 1; i < strings.get(1).length; i++) {
                if (StringUtils.isNotBlank(strings.get(1)[i])) {
                    String tempString = strings.get(1)[i];
                    FedExWeightPrice fedExWeightPrice = new FedExWeightPrice();
                    if (StringUtils.isNotBlank(tempArray[i])) {
                        BigDecimal bigDecimal = new BigDecimal(tempArray[i]);
                        fedExWeightPrice.setPrice(bigDecimal);
                    }
                    fedExWeightPrice.setPartitionId(countryPartitionMaps.get(tempString));
                    fedExWeightPrice.setProductTypeId(productMaps.get(strings.get(0)[0]));
                    fedExWeightPrice.setWeight(Double.valueOf(tempArray[0]));
                    fedExWeightPriceArrayList.add(fedExWeightPrice);
                }

            }
        }
        if (fedExWeightPriceArrayList != null && fedExWeightPriceArrayList.size() > 0) {
            importDataoneMapper.deleteFedEx(productMaps.get(strings.get(0)[0]));
            importDataoneMapper.addFedEx(fedExWeightPriceArrayList);
        }

        return R.ok("导入成功");
    }

    @Override
    public R addDHLChargingSection(List<String[]> strings) {
        Map<String, Long> productMaps = getProductMaps();
        Map<String, Long> countryPartitionMaps = getCountryPartition();
        List<DHLChargingSection> dhlChargingSectionArrayList = new ArrayList<>(strings.size());
        for (int i = 2; i < strings.size(); i++) {
            DHLChargingSection dhlChargingSection = new DHLChargingSection();
            //设置最小重量
            if (StringUtils.isNotBlank(strings.get(i)[0])) {
                dhlChargingSection.setMinWeight(Double.valueOf(strings.get(i)[0]));
            }
            //设置最大重量
            if (StringUtils.isNotBlank(strings.get(i)[1])) {
                dhlChargingSection.setMaxWeight(Double.valueOf(strings.get(i)[1]));
            }
            //设置起始价格
            if (StringUtils.isNotBlank(strings.get(i)[2])) {
                dhlChargingSection.setStartPrice(BigDecimal.valueOf(Double.valueOf(strings.get(i)[2])));
            }
            //设置分区id
            if (StringUtils.isNotBlank(strings.get(i)[3]) && countryPartitionMaps.get(strings.get(i)[3]) != null) {
                dhlChargingSection.setCountryPartitionId(countryPartitionMaps.get(strings.get(i)[3]));
            }
            //设置续重价格
            if (StringUtils.isNotBlank(strings.get(i)[4])) {
                dhlChargingSection.setContinuousPrice(BigDecimal.valueOf(Double.valueOf(strings.get(i)[4])));
            }
            //设置续重重量
            if (StringUtils.isNotBlank(strings.get(i)[5])) {
                dhlChargingSection.setContinuousWeight(Double.valueOf(strings.get(i)[5]));
            }
            //设置产品id
            if (StringUtils.isNotBlank(strings.get(i)[6]) && productMaps.get(strings.get(i)[6]) != null) {
                dhlChargingSection.setProductTypeId(productMaps.get(strings.get(i)[6]));
            }
            dhlChargingSectionArrayList.add(dhlChargingSection);
        }
        if (dhlChargingSectionArrayList != null && dhlChargingSectionArrayList.size() > 0) {
            importDataoneMapper.deleteDHLChargingSection(productMap.get(strings.get(0)[0]));
            importDataoneMapper.addDHLChargingSection(dhlChargingSectionArrayList);
        }
        return R.ok("导入成功");
    }

    public Map<String, Long> getProductMaps() {
        List<ProductType> productTypeList = importDataoneMapper.getProductList();
        for (int i = 0; i < productTypeList.size(); i++) {
            productMap.put(productTypeList.get(i).getProductName(), productTypeList.get(i).getId());
        }
        return productMap;
    }

    public Map<String, Long> getCountryPartition() {
        List<CountryPartition> countryPartitionList = importDataoneMapper.getCountryPartition();
        Map<String, Long> countryPartitionMap = new HashMap<>(countryPartitionList.size());
        for (int i = 0; i < countryPartitionList.size(); i++) {
            countryPartitionMap.put(countryPartitionList.get(i).getPartitionName(), countryPartitionList.get(i).getPartitionId());
        }
        return countryPartitionMap;
    }
}